package com.fly.views.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import com.fly.views.entity.User;

@Controller
public class FreemarkController
{
    @Autowired
    HttpServletRequest request;
    
    // freemarker取值,插值
    @GetMapping("/free1")
    public String free(Model model)
    {
        request.getSession().setAttribute("portal_auth_key", "12212");
        model.addAttribute("intVar", 100);
        model.addAttribute("longVar", 1000000000000000000L);
        model.addAttribute("doubleVar", Math.random());
        model.addAttribute("stringVar", "我是freemarker,是字符串!");
        model.addAttribute("booleanVar", true);
        model.addAttribute("dateVar1", new Date());
        model.addAttribute("nullVar1", null);
        model.addAttribute("nullVar", "我是空");
        return "free1";
    }
    
    @GetMapping("/free2")
    public ModelAndView free2()
    {
        ModelAndView mv = new ModelAndView("free2");
        List<String> list = new ArrayList<>();
        list.add("java");
        list.add("JavaScript");
        list.add("python");
        list.add("php");
        list.add("Html");
        mv.addObject("myList", list);
        return mv;
    }
    
    @GetMapping("free3")
    public ModelAndView free3()
    {
        ModelAndView mv = new ModelAndView("free3");
        Map<String, Object> map = new HashMap<>();
        map.put("Java", "你好Java");
        map.put("address", "北京");
        map.put("身高", 172);
        map.put("money", 100.5);
        mv.addObject("map", map);
        return mv;
    }
    
    @GetMapping("/hello")
    public ModelAndView helloWorld()
    {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("words", "hello你好,world!世界!");
        mv.addObject("userList", this.prepareUserList());
        return mv;
    }
    
    private List<User> prepareUserList()
    {
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 5; i++)
        {
            User item = new User();
            item.setName("用户" + i);
            item.setBirthday(new Date());
            list.add(item);
        }
        return list;
    }
}
